package Askgene::InterfaceModel::Action::Queue::Item::Add;

use warnings;
use strict;

use Askgene::Error;
use Path::Class;
use Reaction::Types::File;
use Reaction::Class;

class Add is 'Reaction::InterfaceModel::Action', which {

    has file => ( isa => 'File', is => 'rw', lazy_fail => 1, required => 1 );

    implements do_apply => as {
        my ($self)   = @_;
        my $queue    = $self->target_model;
        my $upload   = $self->file;
        my $filename = Path::Class::file( $upload->filename )->basename;
        my $item     = eval {
            $queue->add_item(
                {
                    file      => Path::Class::file( $upload->tempname ),
                    filename  => $filename,
                    move_file => 1
                }
            );
        };
        return $item unless $@;
        Askgene::Error->throw(
            500 => $self->ctx->debug
            ? $@
            : q{We had trouble processing your upload, please try again later}
        );
    };

};

1;
